package com.liyuxiang.yygh.order.api;

import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.order.service.AlipayService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/api/order/alipay")
public class AlipayController {
    @Autowired
    AlipayService alipayService;

    @GetMapping("createNative/{orderId}")
    public Result createNative(@ApiParam(name = "orderId", value = "订单id", required = true) @PathVariable("orderId") Long orderId) {
        return Result.ok(alipayService.createNative(orderId));
    }

    @PostMapping("callback")
    public String callback(HttpServletRequest request, HttpServletResponse response) {
        return alipayService.payCallback(request, response);
    }

    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable("orderId") Long orderId) {
        Map<String, Object> resultMap = alipayService.queryPayStatus(orderId);
        if (resultMap == null) {
            return Result.fail().message("支付出错");
        }
        if ("TRADE_SUCCESS".equals(resultMap.get("trade_state"))) {
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }


}
